//
//  MineHeaderView.swift
//  Box
//
//  Created by mn on 2025/3/30.
//
import UIKit
import SnapKit

class MineHeaderView: UIView {
    
    private let backgroundImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "mine_header_bg")
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
    private let avatarImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(systemName: "person.circle")
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.text = "点击登录"
        label.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        label.textColor = .white
        label.isUserInteractionEnabled = true
        label.addTapAction {
            [weak self] in
            UserManager.shared.pushToLogin(self?.findViewController())
        }
        return label
    }()

    let orderView = MineOrderView()
    let mineGoldView = MineGoldView()
    lazy var settingsButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: "setting"), for: .normal)
        button.addTapAction {
            [weak self] in
            let vc = SettingViewController()
            self?.findViewController()?.navigationController?.push(vc)
        }
        return button
    }()
    func reload() {
        guard let user = UserManager.shared.currentUser else {
            nameLabel.text = "点击登录"
            mineGoldView.bottomLabel.text = "0.00"
            nameLabel.isUserInteractionEnabled = true
            return
        }
        nameLabel.isUserInteractionEnabled = false
        nameLabel.text = user.nickname
        mineGoldView.bottomLabel.text = String(format: "%.2f", Float(user.balance) * 0.01)
        
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    private func setupUI() {
        addSubview(backgroundImageView)
        addSubview(avatarImageView)
        addSubview(nameLabel)
        addSubview(settingsButton)
        addSubview(mineGoldView)
        mineGoldView.button.addAction {
            [weak self] in
            let vc = AddGoldViewController()
            self?.findViewController()?.navigationController?.push(vc)
        }
        addSubview(orderView)
        backgroundImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        avatarImageView.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 60, height: 60))
            make.left.equalToSuperview().offset(16)
            make.top.equalTo(settingsButton.snp.bottom).offset(5)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(avatarImageView.snp.right).offset(12)
            make.centerY.equalTo(avatarImageView)
        }
        
        settingsButton.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 40, height: 40))
            make.top.equalTo(safeAreaLayoutGuide.snp.top)
            make.right.equalToSuperview().offset(-16)
        }
        mineGoldView.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.right.greaterThanOrEqualToSuperview()
            make.top.equalTo(avatarImageView.snp.bottom).offset(20)
            make.height.equalTo(98)
        }
        orderView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(mineGoldView.snp.bottom)
            make.height.greaterThanOrEqualTo(135)
        }
    }
}
